package com.alibaba.dubbo.remoting.msgcodec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.alibaba.dubbo.remoting.MessageDecode;
import com.alibaba.dubbo.remoting.MessageEncode;
import com.alibaba.dubbo.remoting.MessageEncodeRegistry;

public class NegativeEncode extends NegativeCodec implements MessageEncode {
	private static final int BUFFER = 4096;
	private static final String CODEC = "neg";
	 
	 
	@Override
	public int encode(InputStream in, OutputStream out/*, MessageEncodeRegistry registry*/) throws IOException {
		int count;
		int length = 0;
		byte data[] = new byte[BUFFER];
		while ((count = in.read(data, 0, BUFFER)) != -1) {
			for(int i = 0;i < count;i++){
				data[i] = (byte)(-data[i]);
			}
			out.write(data, 0, count);
			length += count;
		}

		out.flush();
		/*
		if(registry != null){
			registry.appendEncode(CODEC);
		}
		*/
		return length;
	}
}
